﻿using System;
using System.Linq;
using Microsoft.Practices.ObjectBuilder2;
using Microsoft.Practices.Unity;
using TestByWire.AttributeHandlers;

namespace TestByWire.BuildStrategies
{
	internal class PreBuildRegistrationStrategy : BuilderStrategy
	{
		private readonly IUnityContainer _container;

		public PreBuildRegistrationStrategy(IUnityContainer container)
		{
			if (container == null)
			{
				throw new ArgumentNullException("container");
			}

			_container = container;
		}

		public override void PreBuildUp(IBuilderContext context)
		{
			var pipelineSettings = _container.Resolve<PipelineSettings>();
			var nextStub = new DoNotStub(context.OriginalBuildKey.Type, context.OriginalBuildKey.Name);
			if (pipelineSettings.DoNotStubList.Contains(nextStub))
			{
				var attributeBuildPlan = _container.Resolve<IAttributeBuildPlan>(PipelinePosition.PreCreation.ToString());
				attributeBuildPlan.Execute(context.Existing, _container);
			}
		}
	}
}
